Learn how to build a web3 application using React with AI-generated loot from treasure chests. Utilize an Embedded Wallet for authentication, Cloudflare workers for transaction ease, and Sequence Stack tools.
Gas Sponsoring
and the use of the Transactions API
, you’ll need to upgrade your project plan to Developer
using this walkthrough
Waas Config Key
used for the Embedded Wallet, which can be learned about herePublic Access Key
used for the Embedded Wallet & Transactions API, which can be done hereSecret Access Key
used for the Metadata Service, with the following stepsAccess Settings
Add Service Account
+ Add Service Account
Select Write Permission
Write
and Confirm
copy
the key and store it in a safe location, as you will not have access to this in the future from the Builder Console.mkdir <project>
, then cd <project>
and create a vite
project using React:
/src
First, create a file called something like SequenceEmbeddedWallet.ts
with the following initialization code:
useSessionHash.ts
that generates a session hash from the SDK unique to the user:
GoogleOAuthProvider
to wrap your application. The following command will install it and Apple Auth sign in, which will be used later:
main.tsx
file:
main.tsx
is ready, lets create the login buttons, which will look like this:
App.tsx
use the following code that checks to see if a user is connected and presents a wallet address based on the signed in user, with the various social auth buttons and handlers:
.env
file in the root of your project, adding it to .gitignore
, and updating the file with the following values from the Sequence Builder:
VITE_
to be included in the environment of a vite applicationERC1155
over an ERC721
. The benefits of using an ERC1155
wrangler.toml
with the CONTRACT_ADDRESS
.
Then, for the minting function to work programmatically so that transactions are gasless for your relayer, you will need to make the Transactions API draw from your account credits on your upgraded billing plan by sponsoring your deployed smart contract address.
To allow the Transactions API to relay transactions without a fee, sponsor gas by following this guide for the deployed contract.
ERC1155
contract or simply clone the template specific to this guide.
MINTER_ROLE
for your relayer account address.generate
function within the Cloudflare worker:
getInferenceWithItem
function in order to get the instantiated inference reference from the Scenario API and pass in a prompt
which is the generated loot name
and type
, as well as some additional parameters model parameters, which can be customized via the Scenario API docs:
EulerDiscreteScheduler
scheduler type from the Scenario API based on quality and time, but if you want to experiment with using additional schedulers, you can use this custom local cli and check the results in the Scenario.gg dashboardinferenceId
we can call poll the inference status and return when complete, signified by the succeeded
status descriptor:
resObject.inference.images[0].url
:
url
from the Scenario API in hand, we can move onto storing the asset to the Sequence Metadata Service
. This enables you to link the AI-generated image to the specific token metadata - all through REST-API calls.
Each Dungeon Minter treasure chest reward follows the same process where metadata is first stored using the Sequence Metadata API, the url
and the randomly generated tokenID
(which allows for parallel requests) is returned to the client. The user then consents, after collectible inspection, to mint the token where the tokenID
and the user’s address
passed back to the worker created in Step 5.
tokenID
and url
to the frontend to be rendered, to allow a user to mint after first viewing what they’re minting:
request.headers
for the Referrer
value and compare it against the CLIENT_URL
in the wrangler.toml
:
DAILY_MINT_RESTRICTION
can be set in the wrangler.toml
as a maximum mint allowance per day. And, if you feel it’s necessary to add an ADMIN
to your protocol to be able to mint an infinite amount.
These features can be implemented in the code with the following steps:
hasDailyMintAllowance
is broken down into 2 functions:
fullPaginationDay
of transactions of the users address
mintCount
that corresponds to the from
being the 0x
addresspnpm install @0xsequence/indexer
Then, to implement we use a while loop that gets the first batch of transactions and the page.after
value from the indexer, and continuously checks if the timestamp is less than 24 hours appending to a temporary array for each pass. This ensures we get all of the available transactions:
ERC721
and ERC1155
standard Sequence contracts are from
the 0x
address: